//
//  XMUserManager.h
//  MMCDemo
//
//  Created by zhangdan on 2018/1/10.
//  Copyright © 2018年 zhangdan. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <MMCSDK/MCUser.h>
#import <MMCSDK/MIMCMessage.h>
#import <MMCSDK/MIMCGroupMessage.h>
#import <MMCSDK/MIMCRtsDataType.h>
#import <MMCSDK/MIMCRtsChannelType.h>
#import <UIKit/UIKit.h>

extern int const TIMEOUT_ON_LAUNCHED;
extern int const STATE_INIT;
extern int const STATE_TIMEOUT;
extern int const STATE_AGREE;
extern int const STATE_REJECT;
extern int const CALL_SENDER;
extern int const CALL_RECEIVER;
extern int const CALLID_INVALID;

//接收消息相关
@protocol showRecvMsgDelegate
- (void)onHandleMessage:(NSArray<MIMCMessage*> *)packets user:(MCUser *)user;
- (void)onHandleGroupMessage:(NSArray<MIMCGroupMessage*> *)packets;
- (void)onHandleOnlineMessage:(NSArray<MIMCMessage*> *)packets;
- (void)onHandleUnlimitedGroupMessage:(NSArray<MIMCGroupMessage*> *)packets;
- (void)onHandleServerAck:(MIMCServerAck *)serverAck;
- (void)onHandleOnlineMessageAck:(MCOnlineMessageAck *)serverAck;
- (void)onHandleSendMessageTimeout:(MIMCMessage *)message;
- (void)onHandleSendGroupMessageTimeout:(MIMCGroupMessage *)groupMessage;
- (void)onHandleSendUnlimitedGroupMessageTimeout:(MIMCGroupMessage *)groupMessage;
- (void)onPullNotification;
@end


//处理无限大群相关
@protocol onHandleUnlimitedGroupDelegate
- (void)onHandleCreateUnlimitedGroup:(int64_t)topicId topicName:(NSString *)topicName code:(int)code desc:(NSString *)desc context:(id)context;
- (void)onHandleJoinUnlimitedGroup:(int64_t)topicId code:(int)code desc:(NSString *)desc context:(id)context;
- (void)onHandleQuitUnlimitedGroup:(int64_t)topicId code:(int)code desc:(NSString *)desc context:(id)context;
- (void)onHandleDismissUnlimitedGroup:(int64_t)topicId code:(int)code desc:(NSString *)desc context:(id)context;
- (void)onHandleDismissUnlimitedGroup:(int64_t)topicId;
@end


//用户状态相关
@protocol returnUserStatusDelegate
- (void)onHandleStatusChanged:(MCUser *)user status:(int)status type:(NSString *)type reason:(NSString *)reason desc:(NSString *)desc;
@end

@protocol onCallStateDelegate
- (void)onAnswered:(int64_t)callId accepted:(BOOL)accepted desc:(NSString *)desc;
- (void)onClosed:(int64_t)callId desc:(NSString *)desc;
- (void)onData:(int64_t)callId fromAccount:(NSString *)fromAccount resource:(NSString *)resource data:(NSData *)data dataType:(RtsDataType)dataType channelType:(RtsChannelType)channelType;
@end

@interface XMUserManager : NSObject<parseTokenDelegate, onlineStatusDelegate, handleMessageDelegate, handleRtsCallDelegate, handleUnlimitedGroupDelegate>

@property(nonatomic, weak) id<showRecvMsgDelegate> showRecvMsgDelegate;
@property(nonatomic, weak) id<onHandleUnlimitedGroupDelegate> onHandleUnlimitedGroupDelegate;
@property(nonatomic, weak) id<returnUserStatusDelegate> returnUserStatusDelegate;
@property(nonatomic, weak) id<onCallStateDelegate> onCallStateDelegate;

- (id)init;
+ (XMUserManager *)sharedInstance;


- (BOOL)userLogin:(int64_t)appId appAccount:(NSString *)appAccount;
- (BOOL)userLogin;
- (BOOL)userLogout;


- (void)GDCTimer;
- (NSString *)getAppAccount;
- (void)setAppAccount:(NSString *)appAccount;
- (MCUser *)getUser;
- (void)setUser:(MCUser *)user;
- (void)setToken:(NSString *)token;
- (void)setLoginVC:(UIViewController *)loginVC;


////发送私聊消息
- (void)sendMessage:(NSString *)toAppAccount payload:(NSData *)payload bizType:(NSString *)bizType;

//发送群聊消息
- (void)sendGroupMessage:(int64_t)topicId payload:(NSData *)payload bizType:(NSString *)bizType;

//发送在线消息
- (void)sendOnlineMessage:(NSString *)toAppAccount payload:(NSData *)payload bizType:(NSString *)bizType;

//发送无限大群消息
- (void)sendUnlimitedGroupMessage:(int64_t)topicId payload:(NSData *)payload bizType:(NSString *)bizType;


- (void)parseProxyServiceToken:(void(^)(NSData *data))callback;

- (void)statusChange:(MCUser *)user status:(int)status type:(NSString *)type reason:(NSString *)reason desc:(NSString *)desc;

- (BOOL)handleMessage:(NSArray<MIMCMessage*> *)packets user:(MCUser *)user;
- (BOOL)handleGroupMessage:(NSArray<MIMCGroupMessage*> *)packets;
- (void)handleServerAck:(MIMCServerAck *)serverAck;
- (void)handleSendMessageTimeout:(MIMCMessage *)message;
- (void)handleSendGroupMessageTimeout:(MIMCGroupMessage *)groupMessage;
- (BOOL)handleUnlimitedGroupMessage:(NSArray<MIMCGroupMessage*> *)packets;
- (void)handleSendUnlimitedGroupMessageTimeout:(MIMCGroupMessage *)groupMessage;
- (BOOL)onPullNotification;


- (void)handleCreateUnlimitedGroup:(int64_t)topicId topicName:(NSString *)topicName code:(int)code desc:(NSString *)desc context:(id)context;
- (void)handleJoinUnlimitedGroup:(int64_t)topicId code:(int)code desc:(NSString *)desc context:(id)context;
- (void)handleQuitUnlimitedGroup:(int64_t)topicId code:(int)code desc:(NSString *)desc context:(id)context;
- (void)handleDismissUnlimitedGroup:(int64_t)topicId code:(int)code desc:(NSString *)desc context:(id)context;
- (void)handleDismissUnlimitedGroup:(int64_t)topicId;


- (MIMCLaunchedResponse *)onLaunched:(NSString *)fromAccount fromResource:(NSString *)fromResource callId:(int64_t)callId appContent:(NSData *)appContent;
- (void)onAnswered:(int64_t)callId accepted:(BOOL)accepted desc:(NSString *)desc; // 会话接通之后的回调
- (void)onClosed:(int64_t)callId desc:(NSString *)desc; // 会话被关闭的回调
- (void)onData:(int64_t)callId fromAccount:(NSString *)fromAccount resource:(NSString *)resource data:(NSData *)data dataType:(RtsDataType)dataType channelType:(RtsChannelType)channelType; // 接收到数据的回调
- (void)onSendDataSuccess:(int64_t)callId dataId:(int)dataId context:(id)context;
- (void)onSendDataFailure:(int64_t)callId dataId:(int)dataId context:(id)context;
@end
